/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.allerj.gui.component;

import java.awt.Color;
import java.awt.Component;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.DefaultCellEditor;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 *
 * @author Alessandro Veracchi
 */
public class MyTable extends JTable{
    private String formatterDateE = "d-MMM-yyyy";
    private String patternDateR = formatterDateE;
    private SimpleDateFormat formatterE = new SimpleDateFormat(patternDateR, JComponent.getDefaultLocale());
    private SimpleDateFormat formatterR = formatterE;
    public MyTable(){
        super();
       
        this.setDefaultRenderer( JComponent.class, new JComponentCellRenderer() );
	this.setDefaultEditor( JComponent.class, new JComponentCellEditor() );
        this.setDefaultRenderer( Date.class, new DateRenderer() );
	this.setDefaultEditor( Date.class, new DateEditor() );
        this.setLocale(Locale.ENGLISH);
        this.addMouseListener(new JTableButtonMouseListener(this));
        this.setRowHeight(22);
        
    }
    public TableCellRenderer getCellRenderer(int row, int column) {
            TableColumn tableColumn = getColumnModel().getColumn(column);
            TableCellRenderer renderer = tableColumn.getCellRenderer();
            
            if (renderer == null) {
                    try{
                    Class c = getColumnClass(column);
                    Object o = getValueAt(row,column);
                    if( c.equals(Object.class) )
                    {
                        if( o != null )
                                c = getValueAt(row,column).getClass();
                    }
                    renderer = getDefaultRenderer(c);
                    } catch (Exception e){
                        renderer = getDefaultRenderer(String.class);
                    }
                    
            }
            return renderer;
    }
			
    public TableCellEditor getCellEditor(int row, int column) {
        TableColumn tableColumn = getColumnModel().getColumn(column);
        TableCellEditor editor = tableColumn.getCellEditor();
        if (editor == null) {
                Class c = getColumnClass(column);
                if( c.equals(Object.class) )
                {
                        Object o = getValueAt(row,column);
                        if( o != null )
                                c = getValueAt(row,column).getClass();
                }
                editor = getDefaultEditor(c);
        }
        return editor;
    }
    private class DateRenderer extends DefaultTableCellRenderer { 
        public DateRenderer() { 
            super(); 
        } 
        @Override 
        public void setValue(Object value) {
            formatterR.setLenient(false);
            setText((value == null) ? "" : formatterR.format(value)); 
        } 
    }
     private class DateEditor extends DefaultCellEditor { 
        public DateEditor() { 
            super(new JTextField()); 
        } 
        @Override 
        public boolean stopCellEditing() { 
            String value = ((JTextField)getComponent()).getText();
            if(!value.equals("")) { 
                try { 
                    formatterE.parse(value); 
                } catch (ParseException e) { 
                    ((JComponent)getComponent()).setBorder(new LineBorder(Color.red)); 
                    return false; 
                } 
            } 
            return super.stopCellEditing(); 
        } 
        @Override 
        public Component getTableCellEditorComponent(final JTable table, final Object value, 
        final boolean isSelected, final int row, final int column) { 
            JTextField tf =((JTextField)getComponent()); 
            tf.setBorder(new LineBorder(Color.black)); 
            try { 
                tf.setText(formatterE.format(value)); 
            } catch (Exception e) { 
                tf.setText(""); 
            } 
            return tf; 
        } 
        @Override public Object getCellEditorValue() { 
            try { 
                Date value = formatterE.parse(((JTextField)getComponent()).getText()); 
                return value; 
            } catch (ParseException ex) { 
                return null; 
            } 
        } 
    } 
}
